﻿Imports System.Data.Common

Friend Class FakeDbServices
    Inherits DbProviderServices
    'uses SqlClient provider services to provide all building mechanisms.  could be expanded to manually reflect onto other provider services based on a connection string

    Private ReadOnly _SqlServerYear As String

    Private _SqlProviderServices As DbProviderServices = Nothing
    Private ReadOnly Property SqlProviderServices As DbProviderServices
        Get
            If _SqlProviderServices Is Nothing Then
                Dim sqlProviderServicesType As Type = Type.GetType("System.Data.SqlClient.SqlProviderServices, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                _SqlProviderServices = Activator.CreateInstance(sqlProviderServicesType)
            End If

            Return _SqlProviderServices
        End Get
    End Property

    Public Sub New(sqlServerYear As String)
        _SqlServerYear = sqlServerYear
    End Sub

    Protected Overrides Function CreateDbCommandDefinition(providerManifest As System.Data.Common.DbProviderManifest, commandTree As System.Data.Common.CommandTrees.DbCommandTree) As System.Data.Common.DbCommandDefinition
        Return SqlProviderServices.CreateCommandDefinition(providerManifest, commandTree)
    End Function

    Protected Overrides Function GetDbProviderManifest(manifestToken As String) As System.Data.Common.DbProviderManifest
        Dim sqlProviderManifestType As Type = Type.GetType("System.Data.SqlClient.SqlProviderManifest, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
        Return Activator.CreateInstance(sqlProviderManifestType, {_SqlServerYear})
    End Function

    Protected Overrides Function GetDbProviderManifestToken(connection As System.Data.Common.DbConnection) As String
        Return _SqlServerYear
    End Function
End Class